using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CUtil
{

    public class LogFile : IDisposable
    {
        private bool _disposed;
        StreamWriter writer;
        string logFilePath;
        DateTime lastLog;
        NameObjectCollection timeCollection;

        [DllImport("User32")]
        extern public static int GetGuiResources(IntPtr hProcess, int uiFlags);


        public LogFile(string path)
        {
            _disposed = false;
            timeCollection = new NameObjectCollection();
            if (this.CreateLogFile(path))
            {
                if (this.OpenLogFile())
                {
                    string str1 = " -- Program Start -- ";
                    str1 += DateTime.Now.ToShortDateString() + "---";
                    str1 += DateTime.Now.ToLongTimeString() + " ---";
                    str1 += DateTime.Now.Second;

                    WriteLog(str1);
                }
            }
        }
        public bool WriteTimeLog(string stringToWrite)
        {
            //System.IO.FileStream fs = System.IO.File.OpenWrite(filePath);

            if (writer != null)
            {
                string str = DateTime.Now.ToLongTimeString() + " ---- " + stringToWrite;
                writer.WriteLine(str);
                writer.Flush();

                return true;
            }
            return false;

        }

        public bool WriteTimeSpanLog(string stringToWrite)
        {
            //System.IO.FileStream fs = System.IO.File.OpenWrite(filePath);

            if (writer != null)
            {

                TimeSpan ts = DateTime.Now - this.lastLog;
                lastLog = DateTime.Now;
                string str = ts.TotalMilliseconds + "(ms) ---- " + stringToWrite;
                writer.WriteLine(str);
                writer.Flush();

                return true;
            }
            return false;

        }
        public TimeSpan GetTimeSpan()
        {
            //System.IO.FileStream fs = System.IO.File.OpenWrite(filePath);

            TimeSpan ts = DateTime.Now - this.lastLog;
            lastLog = DateTime.Now;

            return ts;

        }
        public bool WriteLog(string stringToWrite)
        {
            //System.IO.FileStream fs = System.IO.File.OpenWrite(filePath);

            if (writer != null)
            {
                string str = " ---- " + stringToWrite;
                writer.WriteLine(str);
                writer.Flush();
                return true;

            }
            return false;




        }
        private bool CreateLogFile(string path)
        {
            try
            {
                string strDir = path + "\\Log";
                if (!System.IO.Directory.Exists(strDir))
                {
                    System.IO.Directory.CreateDirectory(strDir);
                }
                DateTime dt = DateTime.Now;
                logFilePath = strDir + "\\logfile_" + dt.Year.ToString() + "." + dt.Month.ToString() + "." + dt.Day.ToString() + "_" + dt.Hour.ToString() + "." + dt.Minute.ToString() + "." + dt.Second.ToString() + ".txt";
                System.IO.FileStream fs = System.IO.File.Create(logFilePath);
                fs.Close();
                return true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Could not create logfile " + exc.Message);
                return false;
            }

        }
        private bool OpenLogFile()
        {

            //open logfile
            writer = null;
            try
            {

                lastLog = DateTime.Now;
                writer = new StreamWriter(this.logFilePath);
                return true;
                //writer.WriteLine(indic);


            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Could not write log- file - probably the file is in use !", exc.Message);
                if (writer != null)
                    writer.Close();
                return false;

            }
        }
        public void WriteCurrentTime(string entryName)
        {
            DateTime dt = DateTime.Now;
            timeCollection.Add(entryName, dt);


        }
        public bool Close()
        {
            writer.Close();
            return true;

        }
        public int GetTimeSpanMilliseconds(string entryName)
        {
            object o = timeCollection[entryName];
            if (o != null)
            {

                DateTime dt = (DateTime)(timeCollection[entryName]);
                if (dt != null)
                {
                    TimeSpan ts = DateTime.Now - dt;
                    timeCollection.Remove(entryName);
                    int i = Convert.ToInt32(ts.TotalMilliseconds);

                    return i;


                }
            }
            else
            {
                return -1;
            }
            return 0;

        }
        public static int GetGuiResourcesGDICount()
        {
            return GetGuiResources(Process.GetCurrentProcess().Handle, 0);
        }


        public static int GetGuiResourcesUserCount()
        {
            return GetGuiResources(Process.GetCurrentProcess().Handle, 1);
        }


        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (this.writer != null)
                    {
                        this.writer.Flush();
                        this.writer.Close();
                        this.writer.Dispose();
                    }
                }
                this.writer = null;
                _disposed = true;
            }
        }

        #endregion
    }
}
